import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class StudentAverageScore {

    public static class StudentMapper extends Mapper<LongWritable, Text, Text, Text> {

        private final Text outKey = new Text();
        private final Text outValue = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split(",");
            if (parts.length == 2) { // student.txt
                outKey.set(parts[0]);
                outValue.set("student$" + parts[1]);
                context.write(outKey, outValue);
            } else if (parts.length == 3) { // student_score.txt
                outKey.set(parts[0]);
                outValue.set("score$" + parts[1] + "," + parts[2]);
                context.write(outKey, outValue);
            }
        }
    }

    public static class JoinReducer extends Reducer<Text, Text, Text, Text> {

        private final Text outKey = new Text();
        private final Text outValue = new Text();

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> students = new ArrayList<>();
            List<String> scores = new ArrayList<>();

            for (Text value : values) {
                String[] parts = value.toString().split("\\$");
                if (parts[0].equals("student")) {
                    students.add(parts[1]);
                } else if (parts[0].equals("score")) {
                    scores.add(parts[1]);
                }
            }

            if (!students.isEmpty() && !scores.isEmpty()) {
                for (String student : students) {
                    for (String score : scores) {
                        context.write(null, new Text(key + "," + student + "," + score));
                    }
                }
            }
        }
    }

    public static class AverageScoreMapper extends Mapper<LongWritable, Text, Text, Text> {

        private final Text outKey = new Text();
        private final Text outValue = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split(",");

            outKey.set(parts[0] + "," + parts[1]); // 学号作为 key
            outValue.set(parts[3]); // 分数作为 value
            context.write(outKey, outValue);
        }
    }

    public static class AverageScoreReducer extends Reducer<Text, Text, Text, Text> {

        private final Text outValue = new Text();

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            int count = 0;
            for (Text value : values) {
                sum += Integer.parseInt(value.toString());
                count++;
            }
            double average = (double) sum / count;
            outValue.set(key + "," + String.format("%.2f", average));
            context.write(null, outValue);
        }
    }

    public static class StudentSortComparator implements WritableComparable<StudentSortComparator> {
        private long id;
        private String name;
        private double score;

        @Override
        public int compareTo(StudentSortComparator o) {
            return (int) (o.score - this.score);
        }

        @Override
        public void write(DataOutput dataOutput) throws IOException {
            dataOutput.writeLong(id);
            dataOutput.writeUTF(name);
            dataOutput.writeDouble(score);
        }

        @Override
        public void readFields(DataInput dataInput) throws IOException {
            id = dataInput.readLong();
            name = dataInput.readUTF();
            score = dataInput.readDouble();
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        @Override
        public String toString() {
            return id + "," + name + "," + score;
        }
    }


    public static class SortMapper extends Mapper<LongWritable, Text, StudentSortComparator, Text> {

        private final StudentSortComparator outKey = new StudentSortComparator();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split(",");
            outKey.setId(Long.parseLong(parts[0]));
            outKey.setName(parts[1]);
            outKey.setScore(Double.parseDouble(parts[2]));
            context.write(outKey, new Text(""));
        }
    }

    public static class SortReducer extends Reducer<StudentSortComparator, Text, Text, Text> {
        @Override
        protected void reduce(StudentSortComparator key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            context.write(new Text(key.toString()), new Text(""));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job1 = Job.getInstance(conf, "join");
        job1.setJarByClass(StudentAverageScore.class);
        job1.setMapperClass(StudentMapper.class);
        job1.setReducerClass(JoinReducer.class);
        job1.setOutputKeyClass(Text.class);
        job1.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job1, new Path(args[0]));
        FileOutputFormat.setOutputPath(job1, new Path(args[1] + "/temp"));

        if (!job1.waitForCompletion(true)) {
            System.exit(1);
        }

        Job job2 = Job.getInstance(conf, "average_score");
        job2.setJarByClass(StudentAverageScore.class);
        job2.setMapperClass(AverageScoreMapper.class);
        job2.setReducerClass(AverageScoreReducer.class);
        job2.setOutputKeyClass(Text.class);
        job2.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job2, new Path(args[1] + "/temp"));
        FileOutputFormat.setOutputPath(job2, new Path(args[1] + "/output"));

        if (!job2.waitForCompletion(true)) {
            System.exit(1);
        }

        Job job3 = Job.getInstance(conf, "sort");
        job3.setJarByClass(StudentAverageScore.class);
        job3.setMapperClass(SortMapper.class);
        job3.setReducerClass(SortReducer.class);
        job3.setOutputKeyClass(StudentSortComparator.class);
        job3.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job3, new Path(args[1] + "/output"));
        FileOutputFormat.setOutputPath(job3, new Path(args[1] + "/final_output"));

        System.exit(job3.waitForCompletion(true) ? 0 : 1);
    }
}
